package beans.page;

import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

import models.common.user.DASUser;
import models.common.user.DASUserRole;
import models.midas.form.BaseFormBean;

public abstract class AbstractButton {
	public static final String SAVE_BUTTON_KEY = "save";
	public static final String NEW_BUTTON_KEY = "new";
	public static final String NEW_VER_BUTTON_KEY = "newVer";
	public static final String ADD_GRP_BUTTON_KEY = "addGrp";
	public static final String ABC_BUTTON_KEY = "abc";
	public static final String CANCEL_BUTTON_KEY = "cancel";
	public static final String EDIT_BUTTON_KEY = "edit";
	public static final String ACCEPT_BUTTON_KEY = "accept";
	public static final String DECLINE_BUTTON_KEY = "decline";
	public static final String PROPOSE_BUTTON_KEY = "propose";
	public static final String DELETE_BUTTON_KEY = "delete";
	public static final String MAIL_BUTTON_KEY = "mail";
	
	public static final String ROLE_MMLDESIGNER = "MMLDESIGNER";
	public static final String ROLE_VARKI = "VARKI";
	public static final String ROLE_INTRANETUSER = "INTRANETUSER";
	public static final String ROLE_VARMAN = "VARMAN";
	
	public final String JSON_STATE;
	public final boolean  IS_JSON_AUTHOR;
	public final boolean  JSON_GRAMMAR;
																						
	public final boolean IS_ROLE_MML;
	public final boolean IS_ROLE_VARKI;
	public final boolean IS_ROLE_INTRA;
	public final boolean IS_ROLE_VARMAN;
	
	public AbstractButton(BaseFormBean bean,DASUser user){
		JSON_STATE = bean.getState().toUpperCase();
		IS_JSON_AUTHOR = bean.getAuthorId() == user.id;
		JSON_GRAMMAR = bean.isGrammar();
																							
		IS_ROLE_MML = checkRole(ROLE_MMLDESIGNER,user);
		IS_ROLE_VARKI = checkRole(ROLE_VARKI,user);
		IS_ROLE_INTRA = checkRole(ROLE_INTRANETUSER,user);
		IS_ROLE_VARMAN = checkRole(ROLE_VARMAN,user);
	}
	
	protected abstract boolean isVisible(Object obj);
	protected abstract boolean isVisible();
	protected abstract String getButtonKey();
	public String getButton(Object  obj){
		if(isVisible(obj)){
			return getButtonKey();
		}
		return null;
	}
	
	public String getButton(){
		if(isVisible()){
			return getButtonKey();
		}
		return null;
	}
	
	public boolean checkRole(String roleName,DASUser user){
		Set<DASUserRole> roleSet = user.getDASUserRole();
		Iterator<DASUserRole> it = roleSet.iterator();
		boolean flag = false;
		while (it.hasNext()) {
			if(roleName.equals(it.next().name.toUpperCase())){
				flag = true;
			}
		}
		
		return flag;
	}
}
